package com.education.welco.controller;


import com.education.auth.entity.UserInfo;
import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.utils.BaiduAipContentCensor;
import com.education.utils.JwtUtil;
import com.education.welco.mapper.SAnsweringLikeMapper;
import com.education.welco.pojo.SAnswering;
import com.education.welco.pojo.SAnsweringComment;
import com.education.welco.service.ISAnsweringCommentService;
import com.education.welco.service.ISAnsweringService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * (答疑讨论) 信息操作处理
 *
 * @author d
 * @date 2022-06-20
 */
@RestController
@RequestMapping("/sAnsweringComment")
public class SAnsweringCommentController extends BaseController {

    @Autowired
    private ISAnsweringCommentService sAnsweringCommentService;
    @Autowired
    private ISAnsweringService answeringService;
    @Autowired
    private BaiduAipContentCensor baiduAipContentCensor;
    @Autowired
    private SAnsweringLikeMapper sAnsweringLikeMapper;
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 查询(答疑讨论)列表
     */
    @GetMapping("/list")
    public ApiReturnObject list(SAnsweringComment sAnsweringComment, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SAnsweringComment> list = sAnsweringCommentService.selectSAnsweringCommentList(sAnsweringComment);
        PageInfo pageInfo = new PageInfo<>(list);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }


    /**
     * 答疑，回复详情
     */
    @GetMapping("/listSAnsweringComment")
    public ApiReturnObject listSAnsweringComment(Integer id) {
        if (id != null) {
            SAnswering sAnswering = answeringService.selectSAnsweringById(id);
            //一级 以及 下面子列表
            SAnswering SAnsweringList = listSon2(sAnswering);
            //搜索答疑的讨论人数量
            List<Integer> countEachAnswerTalkPeopleNumNum = sAnsweringCommentService.countEachAnswerTalkPeopleNum(sAnswering.getId());
            SAnsweringList.setTotal(countEachAnswerTalkPeopleNumNum.size());
            //答疑讨论总数量
            Integer countEachAnswerTalkNum = sAnsweringCommentService.countEachAnswerTalkNum(Integer.valueOf(sAnswering.getId()));
            SAnsweringList.setCommentNum(countEachAnswerTalkNum);
            return new ApiReturnObject("200", "查询成功", SAnsweringList);
        } else {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
    }


    //查询目录列表
    public SAnswering listSon2(SAnswering sAnswering) {
        //根据第一级的id查询出下级列表
        SAnsweringComment sAnsweringComment = new SAnsweringComment();
        sAnsweringComment.setCPostId(sAnswering.getId());
        List<SAnsweringComment> jrCatalogs = sAnsweringCommentService.selectSAnsweringCommentList(sAnsweringComment);
        UserInfo userInfo = jwtUtil.getInfoFromToken();
        //回调点赞 查询当前用户的点赞评论id列表    目的是给查询出的评论做标记(当前用户点赞过哪条评论)
        List<String> likeItIds = sAnsweringLikeMapper.selectForumsIdsByUserIdAndType(String.valueOf(userInfo.getId()), "5");
        setLikeStatus(jrCatalogs, likeItIds);
        if (jrCatalogs != null && jrCatalogs.size() > 0) {
            //设置子级集合
            sAnswering.setChildren(listSon(jrCatalogs));
        }
        return sAnswering;
    }

    //查询目录列表
    public List<SAnsweringComment> listSon(List<SAnsweringComment> cList) {
        for (SAnsweringComment one : cList) {
            SAnsweringComment sAnsweringComm = new SAnsweringComment();
            sAnsweringComm.setCPostId(one.getCPlId());
            List<SAnsweringComment> jrCatalogs = sAnsweringCommentService.selectSAnsweringCommentList(sAnsweringComm);
            UserInfo userInfo = jwtUtil.getInfoFromToken();
            //回调点赞 查询当前用户的点赞评论id列表    目的是给查询出的评论做标记(当前用户点赞过哪条评论)
            List<String> likeItIds = sAnsweringLikeMapper.selectForumsIdsByUserIdAndType(String.valueOf(userInfo.getId()), "5");
            setLikeStatus(jrCatalogs, likeItIds);
            if (jrCatalogs != null && jrCatalogs.size() > 0) {
                //设置子级集合
                one.setChildren(listSon(jrCatalogs));
            }
        }
        return cList;
    }

    private void setLikeStatus(List<SAnsweringComment> list, List<String> likeItIds) {
        //判断是否点赞    设置子级评论列表
        for (SAnsweringComment sysForumsCommentResq : list) {
            String commentId = String.valueOf(sysForumsCommentResq.getCPlId());
            if (likeItIds.contains(commentId)) {
                //已经点赞
                sysForumsCommentResq.setIsLike(1);
            } else {
                //没有点赞
                sysForumsCommentResq.setIsLike(0);
            }
        }
    }

    /**
     * 备课中心答疑
     * 新增保存-答疑讨论
     *
     * @param sAnsweringComment
     * @return
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SAnsweringComment sAnsweringComment) {
        sAnsweringComment.setTCreateTime(new Date());
        //屏蔽词过滤为***
        if (StringUtils.isNotNull(sAnsweringComment.getCPlText())) {
            //text = (filter.replaceSensitiveWord(text, 1, "*"));
            int i = baiduAipContentCensor.AipText(sAnsweringComment.getCPlText());
            if (i == 2) {
                return new ApiReturnObject("201", "您发表言论不当,评论发表失败!", "");
            }
        }
        sAnsweringCommentService.insertSAnsweringComment(sAnsweringComment);
        return new ApiReturnObject("200", "评论添加成功", "");
    }


    /**
     * 详情(答疑讨论)
     */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id) {
        SAnsweringComment result = sAnsweringCommentService.selectSAnsweringCommentById(id);
        return new ApiReturnObject("200", "查询成功", result);
    }

    /**
     * 修改保存(答疑讨论)
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SAnsweringComment sAnsweringComment) {
        sAnsweringCommentService.updateSAnsweringComment(sAnsweringComment);
        return new ApiReturnObject("200", "修改成功", "");
    }


    /**
     * 置顶(答疑讨论)
     */
    @GetMapping("/stick")
    @ApiOperation("置顶|取消置顶(答疑讨论)")
    @ApiImplicitParam(name = "stick", value = "是否置顶 0 未置顶 1 已置顶", dataType = "Integer")
    public ApiReturnObject stickSave(Integer cPlId, Integer stick) {
        sAnsweringCommentService.stickSAnsweringCommentForTeacher(cPlId, stick);
        String msg = "置顶成功！";
        if (stick == 0) {
            msg = "取消置顶成功！";
        }
        return new ApiReturnObject("200", msg, stick);
    }
    //public ApiReturnObject stickSave(Integer cPostId, Integer cPlId) {
    //    sAnsweringCommentService.stickSAnsweringComment(cPostId, cPlId);
    //    return new ApiReturnObject("200", "修改成功", "");
    //}

    /**
     * 删除(答疑讨论)
     */
    @GetMapping("/del")
    public ApiReturnObject remove(String ids) {
        sAnsweringCommentService.deleteSAnsweringCommentByIds(ids);
        return new ApiReturnObject("200", "删除成功", "");
    }
}